/*
 * MClickableImage.h
 *
 *  Created on: Sep 4, 2010
 *      Author: alex
 */

#ifndef MCLICKABLEIMAGE_H_
#define MCLICKABLEIMAGE_H_

#include <MImageWidget>
#include <QPropertyAnimation>
#include <QGraphicsSceneMouseEvent>
#include <MAnimation>
#include "../MeegoSupport.h"
#include <QGraphicsBlurEffect>
#include <MLabel>
#include <QDebug>

class FadeAnimationStyleContainer : public MAnimationStyleContainer {};

class FadeAnimation : public MAnimation
{
    Q_OBJECT
    M_ANIMATION(FadeAnimationStyle)

public:
    FadeAnimation(QGraphicsItem *item, QObject *parent = NULL) : MAnimation(parent),mItem(item) {}
    virtual void updateCurrentTime (int currentTime ) {
        mItem->setOpacity(currentTime/1000.0);
    }
    virtual int duration () const { return 1000; }

protected:
    QGraphicsItem *mItem;
};

class MClickableImage : public MImageWidget
{
	Q_OBJECT;

public:
        MClickableImage(MWidget* parent = 0)
            : MImageWidget(parent), isClickAnimEnabled(false), isBorderEnabled(false) {
		setAspectRatioMode(Qt::KeepAspectRatioByExpanding);
		//setAttribute(Qt::WA_TranslucentBackground);
                anim = new FadeAnimation(this);
	}

        MClickableImage(const QPixmap& pixmap, MWidget* parent = 0)
            : MImageWidget(parent), isClickAnimEnabled(false), isBorderEnabled(false) {
                setAspectRatioMode(Qt::KeepAspectRatioByExpanding);
                setPixmap(pixmap);
                //setAttribute(Qt::WA_TranslucentBackground);
                anim = new FadeAnimation(this);
        }

	~MClickableImage() { delete anim; }

	void setBorderEnabled(bool borderEnabled) { isBorderEnabled = borderEnabled; }
	void setClickAnimEnabled(bool clickAnimEnabled) { isClickAnimEnabled = clickAnimEnabled; }
	void mousePressEvent(QGraphicsSceneMouseEvent* e)
	{
		//this->setFrameStyle(QFrame::Box);
		//this->setGraphicsEffect(new QGraphicsBlurEffect(this));
		if(isClickAnimEnabled)
                        setGeometry(QRectF(QPointF(geometry().x(), geometry().y()), size() - QSizeF(2,2)));

		mousePos = e->pos();
		isMousePressed = true;
		e->accept();
		emit mousePressed();
                //update();
                //MImageWidget::mousePressEvent(e);
	}

	void mouseReleaseEvent(QGraphicsSceneMouseEvent* e)
	{
		//this->setFrameStyle(QFrame::NoFrame);
		//setFixedSize(size() + QSize(2,2));
		if(isClickAnimEnabled)
                    setGeometry(QRectF(QPointF(geometry().x(), geometry().y()), size() + QSizeF(2,2)));

		//this->setGraphicsEffect(NULL);
		emit mouseReleased();

		if(isMousePressed)
		{
			isMousePressed = false;
                        //MeegoSupport::MSMessageBox("CLicked", "Clicked");
			emit clicked();
		}

                //MImageWidget::mouseReleaseEvent(e);
                //update();
	}

	void mouseMoveEvent(QGraphicsSceneMouseEvent* e)
	{
		//qDebug() << geometry() << e->pos() << e->globalPos() << e->x() << e->y();
		//if(!geometry().contains(e->globalPos()))
		QRectF r(mousePos, e->pos());
		//qDebug() << r.height() << r.width();
		if(abs((int)r.height()) > 20 || abs((int)r.width()) > 20)
			isMousePressed = false;

		MImageWidget::mouseMoveEvent(e);
	}


	void paint(QPainter * painter, const QStyleOptionGraphicsItem * option) {
		if(isBorderEnabled)
		{
			if(isMousePressed) {
				//qDebug() << "Paint!";
				//painter.setBrush(QBrush(QColor("#37b4fc")));
				painter->setRenderHint(QPainter::Antialiasing);
				painter->setPen(QPen(QColor("#37b4fc"), 2));
				painter->drawRoundedRect(QRectF(QPoint(1, 1), size() - QSize(2,2)), 8, 8);
			} else {
				//painter.setPen(QPen(QBrush(palette().color(QPalette::Background)), 2));
			}
			//painter.drawRoundedRect(QRect(QPoint(0, 0), size()), 2, 2);
		}

		MImageWidget::paint(painter, option);
	}

        QSizeF sizeHint ( Qt::SizeHint which, const QSizeF & constraint = QSizeF() ) const {
            return imageSize();
        }

	void animHide() {
		//qDebug() << __FUNCTION__ << width() << height() << geometry() << size();
                QObject::connect(anim, SIGNAL(finished()), this, SLOT(hide()), Qt::QueuedConnection);
                //anim->setStartValue(QRectF(QPointF(geometry().x(), geometry().y()), size()));
                //anim->setEndValue(QRectF(geometry().x() /*+ size().width() / 2*/, geometry().y() /*+ size().height() / 2*/, 0, 0));
                //anim->setDuration(150);
                anim->setDirection(QAbstractAnimation::Backward);
		anim->start();
	}

        void animShow(const QSizeF& finalSize = QSizeF(48, 48)) {
                setGeometry(QRectF(QPointF(geometry().x(), geometry().y()), /*QSizeF(0,0)*/finalSize));
                setOpacity(0.0);
		//qDebug() << __FUNCTION__ << width() << height() << geometry() << size();
                show();
                QObject::disconnect(anim, SIGNAL(finished()), this, SLOT(hide()));
                //anim->setStartValue(QRectF(geometry().x() /*+ finalSize.width() / 2*/, geometry().y() /*+ finalSize.height() / 2*/, 0, 0));
                //anim->setEndValue(QRectF(QPointF(geometry().x(), geometry().y()), finalSize));
                //anim->setDuration(150);
                //anim->start();
                anim->setDirection(QAbstractAnimation::Forward);
                anim->start();
	}

signals:
	void mousePressed();
	void mouseReleased();
	void clicked();
private:
	QPointF	mousePos;
	bool isMousePressed;
	bool isBorderEnabled;
	bool isClickAnimEnabled;
        //QPropertyAnimation* anim;
        FadeAnimation* anim;
};

#endif /* MCLICKABLEIMAGE_H_ */
